<?php

/**
 * overview actions.
 *
 * @package    ppstool
 * @subpackage overview
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class overviewActions extends sfActions
{
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeIndex(sfWebRequest $request)
	{
		$this->getUser()->setAttribute('progress', 100);

		if (!$this->getUser()->getAttribute('xmlin')) {
			$this->getUser()->setFlash('error', "Bitte zuerst den Import ausführen!", true);
			$this->redirect("import/index");
		} else {

			//Add structure to xml
			$xmlstring = "<input>
	<qualitycontrol type='no' losequantity='0' delay='0' />
	<sellwish></sellwish>
	<selldirect></selldirect>
	<orderlist></orderlist>
	<productionlist></productionlist>
	<workingtimelist></workingtimelist>
</input>";

			$xmlout = new SimpleXMLElement($xmlstring);

			/*
			 * Stock Amount
			 */

			$this->stockamount = $this->getUser()->getAttribute('sa');

			/*SELLWISHES
			 * ----------------------------------------------------
			 * <item article="3" quantity="100" />
			 */

			$this->p1n = $this->addSellwish($xmlout, 1);
			$this->p2n = $this->addSellwish($xmlout, 2);
			$this->p3n = $this->addSellwish($xmlout, 3);

			/* DIRECT SALES
			 * -----------------------------------------------
			 * <item article="1" quantity="0" price="0.0" penalty="0.0" />
			 */

			$this->addDirectSales($xmlout, 1);
			$this->addDirectSales($xmlout, 2);
			$this->addDirectSales($xmlout, 3);


			/*
			 * ORDER LIST
			 * <order article="21" quantity="440" modus="4" />
			 *  21, 23, 25, 33, 39, 52, 53, 59
			 */

			//$this->orderItems = array(21, 23, 25, 33, 39, 52, 53, 59);
			$this->orderList = $this->getUser()->getAttribute('ol');

			if (!$this->orderList) {
				$this->getUser()->setFlash('error', "Bitte zuerst die Berechnung ausführen!", true);
				$this->redirect("production/safetystock");
			}

			for ($item = 0; $item<105; $item++) {
				$this->addOrder($xmlout, $item, $this->orderList);
			}

			/*
			 * productionlist
			 * --------------------------------------------------------
			 * 17, 17, 26, 17, 26, 17, 26, 51, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
			 * 11, 12, 13, 14, 18, 16, 19, 20, 29, 30, 31, 49, 50, 51, 54, 55, 56
			 *
			 */

			$this->articles = array(18, 13, 7, 49, 10, 4, 50, 17, 16, 51, 26,
			1, 19, 14, 8, 54, 11, 5, 55, 17, 16, 56, 26, 2, 20, 15, 9, 29, 12,
			6, 30, 17, 16, 31, 26, 3);

			$this->sortiertereihenfolgeitems = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,26,49,54,29,50,55,30,51,56,31);

			$this->productionlist = $this->getUser()->getAttribute('as');

			foreach ($this->articles as $production) {
				$this->addProduction($xmlout, $production, $this->productionlist);
			}

			/*
			 * Workingtime
			 * ---------------------------------------------------------
			 * <workingtime station="1" shift="1" overtime="75" />
			 */
			$this->workingstations = $this->getUser()->getAttribute('workingtime');

			for ($itemno=1; $itemno<=15; $itemno++) {
				if ($itemno!=5) {
					$this->addWorkingtime($xmlout, $itemno, $this->workingstations);
				}
			}


			//Save to session for export
			$this->getUser()->setAttribute('xmlout', $xmlout->asXML());

		}
	}


	public function addSellwish ($xmlout, $itemno) {
		$item = $xmlout->sellwish->addChild('item');
		$item->addAttribute('article', $itemno);
		$quantity = $this->getUser()->getAttribute('p'.$itemno.'n');
		$item->addAttribute('quantity', $quantity);
		return $quantity;
	}

	public function addDirectSales($xmlout, $itemno) {
		$item = $xmlout->selldirect->addChild('item');
		$item->addAttribute('article', $itemno);
		$this->directquantity = $this->getUser()->getAttribute('p'.$itemno.'directquantity');
		if ($this->directquantity)
		{
			$item->addAttribute('quantity', $this->directquantity);
		}
		else
		{
			$item->addAttribute('quantity', 0.0);
		}
		$this->directprice = $this->getUser()->getAttribute('p'.$itemno.'directprice');
		if ($this->directprice)
		{
			$item->addAttribute('price', $this->directprice);
		}
		else
		{
			$item->addAttribute('price', 0.0);
		}
		$this->directpenalty = $this->getUser()->getAttribute('p'.$itemno.'directpenalty');
		if ($this->directpenalty)
		{
			$item->addAttribute('penalty', $this->directpenalty);
		}
		else
		{
			$item->addAttribute('penalty', 0.0);
		}
	}

	public function addOrder($xmlout, $itemno, $orderlist) {

		if(array_key_exists($itemno, $orderlist) && $orderlist[$itemno]["amount"]>0) {
			$item = $xmlout->orderlist->addChild('item');
			$item->addAttribute('article', $itemno);
			$this->poq = $orderlist[$itemno]["amount"];
			$item->addAttribute('quantity', $this->poq);
			$this->pom = $orderlist[$itemno]["modus"];
			$item->addAttribute('modus', $this->pom);
		}

	}

	/*
	 * <production article="17" quantity="300" />
	 */
	public function addProduction($xmlout, $itemno, $list) {
		if ($list[$itemno]>0) {
			$item = $xmlout->productionlist->addChild('item');
			$item->addAttribute('article', $itemno);
			if ($itemno == 16 || $itemno == 17 || $itemno == 26)
			{
				$item->addAttribute('quantity', ceil($list[$itemno]/3));
			}
			else
			{
				$item->addAttribute('quantity', $list[$itemno]);
			}
		}
	}

	/*
	 * <workingtime station="1" shift="1" overtime="75" />
	 */
	public function addWorkingtime($xmlout, $itemno, $workingstations) {
		//workingtime
		$item = $xmlout->workingtimelist->addChild('item');
		$item->addAttribute('station', $itemno);
		$this->shift = $workingstations[$itemno]["shift"];
		$item->addAttribute('shift', $this->shift);
		$this->overtime = $workingstations[$itemno]["overtime"];
		$item->addAttribute('overtime', $this->overtime);
	}

	public function executeCalculate(sfWebRequest $request)
	{
		$this->getUser()->setAttribute('progress', 50);
	}

	public function executeImpressum(sfWebRequest $request)
	{
	}
}
